home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / viewjpeg.ps < prev    next >
Text File  |  1996-01-10  |  4KB  |  143 lines

  1. %! viewjpeg.ps   Copyright (C) Thomas Merz 1994
  2. %
  3. % View JPEG files with Ghostscript
  4. %
  5. % This PostScript code relies on level 2 features.
  6. %
  7. % Only JPEG baseline, extended sequential, and progressive files
  8. % are supported.  Note that Adobe PostScript level 2 does not include
  9. % progressive-JPEG support.  Ghostscript with IJG JPEG v6 or later
  10. % will decode progressive JPEG, but only if you edit gsjmorec.h to
  11. % enable that feature.
  12. %
  13. % Author's address:
  14. % ------------------------------+
  15. % {(pstack exec quit) = flush } |    Thomas Merz, Munich
  16. % pstack exec quit              |    voice +49/89/29160728
  17. % ------------------------------+    tm@muc.de  http://www.muc.de/~tm/
  18. %
  19. % Updates by Tom Lane 6-Sep-95
  20.  
  21. /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
  22.   (JPEG needs PostScript Level 2!\n) print flush stop
  23. } if
  24.  
  25. /JPEGdict 20 dict def
  26. JPEGdict begin
  27.  
  28. /NoParamMarkers [    % JPEG markers without additional parameters
  29.     16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
  30. ] def
  31.  
  32. /NotSupportedMarkers [     % JPEG markers not supported by PostScript level 2
  33.     16#C3 16#C5 16#C6 16#C7 16#C8 16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
  34. ] def
  35.  
  36. % Names of color spaces
  37. /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
  38.  
  39. % read one byte from file F
  40. % - ==> int --or-- stop context
  41. /NextByte { 
  42.     F read not { (Read error in ViewJPEG!\n) print flush stop } if
  43. } bind def
  44.  
  45. /SkipSegment {    % read two bytes and skip that much data
  46.     NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
  47. } bind def
  48.  
  49. % read width, height, and # of components from JPEG markers
  50. % and store in dict
  51. /readJPEGmarkers {    % - ==> dict --or-- stop context
  52.     5 dict begin
  53.  
  54.     { % loop: read JPEG marker segments until we find SOFn marker or EOF
  55.     NextByte
  56.     16#FF eq {                % found marker
  57.         /markertype NextByte def
  58.         % Is it S0F0=baseline, SOF1=extended sequential, SOF2=progressive ?
  59.         markertype dup 16#C0 ge exch 16#C2 le and {
  60.         NextByte pop NextByte pop    % segment length
  61.         % Ghostscript and Adobe PS accept only data precision 8
  62.         NextByte 8 ne {
  63.             (Error: not 8 bits per component!\n) print flush stop 
  64.         } if
  65.  
  66.         % Read crucial image parameters
  67.         /height NextByte 8 bitshift NextByte add def
  68.         /width NextByte 8 bitshift NextByte add def
  69.         /colors NextByte def
  70.  
  71.         DEBUG { currentdict { exch == == } forall flush } if
  72.         exit
  73.         } if
  74.  
  75.         % detect several segment types which are not compatible with PS
  76.         NotSupportedMarkers {
  77.         markertype eq {
  78.             (Marker ) print markertype == 
  79.             (not supported!\n) print flush stop
  80.         } if 
  81.         } forall 
  82.  
  83.         % Skip segment if marker has parameters associated with it
  84.         true NoParamMarkers { markertype eq {pop false exit} if } forall 
  85.         { SkipSegment } if
  86.     } if
  87.     } loop
  88.  
  89.     currentdict dup /markertype undef
  90.     end
  91. } bind def
  92.  
  93. end    % JPEGdict
  94.  
  95. % read image parameters from JPEG file and display the image
  96. /viewJPEG {        % <file|string> ==> -
  97.     save 
  98.     JPEGdict begin
  99.     /saved exch def
  100.     /scratch 1 string def
  101.     dup type /stringtype eq { (r) file } if
  102.     /F exch def
  103.  
  104.     readJPEGmarkers begin
  105.     F 0 setfileposition        % reset file pointer
  106.  
  107.     % We use the whole clipping area for the image (at least in one dimension)
  108.     gsave clippath pathbbox grestore
  109.     /ury exch def /urx exch def
  110.     /lly exch def /llx exch def
  111.  
  112.     llx lly translate
  113.     width height scale
  114.  
  115.     % use whole width or height, whichever is appropriate
  116.     urx llx sub width div ury lly sub height div
  117.     2 copy gt { exch } if pop        % min
  118.     dup scale
  119.     ColorSpaceNames colors scratch cvs get setcolorspace
  120.  
  121.     % prepare image dictionary
  122.     << /ImageType 1
  123.        /Width width
  124.        /Height height
  125.        /ImageMatrix [ width 0 0 height neg 0 height ]
  126.        /BitsPerComponent 8
  127.        % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
  128.        colors 4 eq {
  129.          /Decode [ colors { 1 0 } repeat ]
  130.        } {
  131.          /Decode [ colors { 0 1 } repeat ]
  132.        } ifelse
  133.        /DataSource F /DCTDecode filter
  134.     >> image
  135.  
  136.     end        % image parameter dictionary
  137.  
  138.     saved end restore
  139. } bind def
  140.  
  141. % usage example:
  142. % (jpeg-6/testimg.jpg) viewJPEG
  143.